﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Http;

namespace Url
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private async void btnScrape_Click(object sender, EventArgs e)
        {
            btnScrape.Enabled = false;
            rtbResult.Text = "抓取中，请稍候...";
            string url = txtUrl.Text.Trim();
            if (string.IsNullOrEmpty(url))
            {
                MessageBox.Show("请输入URL！");
                return;
            }

            try
            {
                using (HttpClient client = new HttpClient())
                {
                    client.Timeout = TimeSpan.FromSeconds(10); // 设置超时
                    string html = await client.GetStringAsync(url);

                    MatchCollection emails = Regex.Matches(html, @"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b");
                    MatchCollection phones = Regex.Matches(html, @"\b1[3-9]\d{9}\b"); // 简单匹配中国大陆手机号

                    rtbResult.Clear();
                    rtbResult.AppendText("=== 邮箱地址 ===\n");
                    foreach (Match email in emails)
                    {
                        rtbResult.AppendText(email.Value + "\n");
                    }

                    rtbResult.AppendText("\n=== 手机号码 ===\n");
                    foreach (Match phone in phones)
                    {
                        rtbResult.AppendText(phone.Value + "\n");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"错误: {ex.Message}");
            }

            finally
            {
                btnScrape.Enabled = true;
            }
        }

        private void btnScrape_Click_1(object sender, EventArgs e)
        {
            btnScrape_Click(sender, e);
        }
    }
}
